<?php 
	class Topics extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_subject(){
			$sql = "SELECT * FROM {$this->db->dbprefix}b_subject";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}

		function get_chapter($conditions = array()){
			$where = "WHERE 1=1";
			if(isset($conditions['subject_id'])){
				$where .= " AND subject_id = '{$conditions['subject_id']}'";
			}
			$sql = "SELECT * FROM {$this->db->dbprefix}b_subject_chapter $where";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}

		function get_topic_list($limit = 0, $offset = 0, $conditions = array()){
			$where = "1 = 1";
			$order = " t.id DESC ";
			if(!empty($text)) $where .= " AND subject_text LIKE '%{$text}%'";
			if(count($conditions) > 0){
				if(!empty($conditions['source_type'])){
					$where .= " AND t.source_type = '{$conditions['source_type']}'";
				}
				if(!empty($conditions['subject_id'])){
					$where .= " AND t.subject_id = '{$conditions['subject_id']}'";
				}
				if(!empty($conditions['chapter_id'])){
					$where .= " AND t.chapter_id = '{$conditions['chapter_id']}'";
				}
				if(!empty($conditions['type'])){
					$where .= " AND t.type = '{$conditions['type']}'";
				}
				if(!empty($conditions['subject_text'])){
					$where .= " AND t.subject_text LIKE '%{$conditions['subject_text']}%'";
				}
				if(!empty($conditions['id'])){
					$where .= " AND t.id IN({$conditions['id']})";
				}
				if(!empty($conditions['order'])){
					$order = $conditions['order'];
				}
				if(!empty($conditions['join'])){
					if(isset($conditions['is_right'])){
						$where .= " AND th.id IS NOT NULL AND th.is_right = '{$conditions['is_right']}' ";
					}else{
						$where .= " AND th.id IS NULL ";
					}
				}
				if(!empty($conditions['school_id']) && !empty($conditions['uid'])){
					$time = time();
                    $where .= " AND (t.school_id = '{$conditions['school_id']}' OR school_id = 0)";
				}
			}
			$sql = "SELECT SQL_CALC_FOUND_ROWS t.* FROM {$this->db->dbprefix}tk_topics AS t ";
			if(!empty($conditions['join'])){
				$sql .= "LEFT JOIN {$this->db->dbprefix}tk_test_topic_historys AS th ON(th.topic_id=t.id AND th.volume_id=0 AND th.volume_history_id=0 AND th.uid='{$conditions['uid']}' )";
			}
			$sql .= "WHERE $where ORDER BY $order ";
			if($limit > 0 || $offset > 0){
				$sql .= "LIMIT $limit, $offset";
			}
			$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "result" => $result);
		}

		function get_topic_reuslt($topic_id){
			$sql = "SELECT * FROM {$this->db->dbprefix}tk_topic_result WHERE topic_id = $topic_id ORDER BY id ASC";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}

		function get_test_volume_templates($school_id){
			$sql = "SELECT * FROM {$this->db->dbprefix}tk_test_volume_templates WHERE school_id = '{$school_id}' ORDER BY id DESC";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}

		function get_topic_analys($topic_id){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_topic_analys WHERE topic_id = $topic_id ORDER BY id ASC ";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_topic_analys_result($aid){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_topic_analys_result WHERE aid = $aid ORDER BY id ASC ";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_analys_result_by_aid($topic_id, $aid){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_topic_analys_result WHERE topic_id='{$topic_id}' AND aid IN({$aid})";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_topic_by_id($topic_id){
            $sql = "SELECT id,type,subject_text,analysis,difficulty_level FROM {$this->db->dbprefix}tk_topics WHERE id IN($topic_id)";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_topic_result_by_id($topic_id){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_topic_result WHERE topic_id IN($topic_id)";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_topic_calc($topic_id){
        	$sql = "SELECT * FROM {$this->db->dbprefix}tk_topics_calc WHERE topic_id = '{$topic_id}'";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }
	}